package cn.wolfcode.wolf2w.util;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.query.DestinationObject;
import cn.wolfcode.wolf2w.query.QueryObject;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.List;

public class DBHelper {

    public static <T> Page<T> query(MongoTemplate template, Query query, Pageable pageable, Class<T> clz, QueryObject qo) {

        long total = template.count(query, clz);
        if (total == 0) {
            return Page.empty();
        }

        query.with(pageable);

        List<T> list = template.find(query, clz);
        return new PageImpl<>(list, pageable, total);
    }
}
